/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AddPicAlbum.java
 *
 * Created on 05-mar-2009, 21:49:42
 */
package edu.upf.grupE.interftest;

import edu.upf.grupE.Controller.AlbumController;
import edu.upf.grupE.Controller.PictureController;
import edu.upf.grupE.Entities.Album;
import edu.upf.grupE.Entities.FileDescriptor;
import edu.upf.grupE.Entities.Picture;
import edu.upf.grupE.Entities.Tag;
import edu.upf.grupE.Entities.TextDescription;
import edu.upf.grupE.impl.GUILogic;
import java.io.File;
import java.util.Vector;

/**
 *
 * @author Oscar
 */
public class AddPicAlbum extends javax.swing.JFrame {

    Album album;
    ImageSelection im;
    String path;
    boolean auxiliars;
    VideoSelection vid;

    /** Creates new form AddPicAlbum */
    public AddPicAlbum(ImageSelection im, String path, VideoSelection vid) {
        this.vid = vid;
        this.im = im;
        this.path = path;
        auxiliars = true;
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        cmb_ontologyes = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        txt_author = new javax.swing.JTextField();
        txt_picname = new javax.swing.JTextField();
        txt_desc = new javax.swing.JTextField();
        txt_keys = new javax.swing.JTextField();
        btn_ok = new javax.swing.JButton();
        btn_cancel = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        lbl_newAlbum = new javax.swing.JTextField();
        cmb_albums = new javax.swing.JComboBox();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        txt_ontologyterm = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Add a Picture to an Album");

        jLabel1.setText("TAG");

        Vector <String> ontologies= GUILogic.getOntologyes();

        for(int i=0; i<ontologies.size(); i++){
            cmb_ontologyes.addItem(ontologies.get(i));
        }

        jLabel2.setText("Author:");

        jLabel3.setText("Name:");

        jLabel4.setText("DESCRIPTION");

        jLabel5.setText("Description:");

        jLabel6.setText("KeyWord:");

        btn_ok.setText("OK");
        btn_ok.setBorderPainted(false);
        btn_ok.setContentAreaFilled(false);
        btn_ok.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_okActionPerformed(evt);
            }
        });

        btn_cancel.setText("Cancel");
        btn_cancel.setBorderPainted(false);
        btn_cancel.setContentAreaFilled(false);
        btn_cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_cancelActionPerformed(evt);
            }
        });

        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel7.setText("ALBUM:");

        jLabel8.setText("Albums:");

        jLabel9.setText("New Album:");

        Vector <Album> albums= GUILogic.getListAlbums();

        for(int i=0; i<albums.size(); i++){
            if(albums.get(i).getId()!=1L){
                cmb_albums.addItem(albums.get(i).getName());
            }
        }
        //DefaultComboBoxModel model= new DefaultComboBoxModel();

        jLabel10.setFont(new java.awt.Font("Tahoma", 1, 8));
        jLabel10.setForeground(new java.awt.Color(255, 51, 51));
        jLabel10.setText("Keywords:Comma separated Values");

        jLabel11.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel11.setForeground(new java.awt.Color(255, 51, 51));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel11)
                        .addContainerGap())
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel7)
                            .addContainerGap())
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel3)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(txt_picname, javax.swing.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE))
                                .addGroup(layout.createSequentialGroup()
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(jLabel5)
                                        .addComponent(jLabel6)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel1)
                                            .addComponent(jLabel4)))
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(txt_keys)
                                        .addComponent(txt_desc, javax.swing.GroupLayout.PREFERRED_SIZE, 260, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jLabel10)
                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                            .addComponent(txt_ontologyterm, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addGap(76, 76, 76))))
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel2)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                            .addComponent(jLabel8)
                                            .addGap(18, 18, 18)
                                            .addComponent(cmb_albums, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addComponent(txt_author, javax.swing.GroupLayout.DEFAULT_SIZE, 375, Short.MAX_VALUE)
                                        .addGroup(layout.createSequentialGroup()
                                            .addComponent(jLabel9)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                            .addComponent(lbl_newAlbum, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE))))
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(78, 78, 78)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(cmb_ontologyes, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                            .addComponent(btn_ok)
                                            .addGap(51, 51, 51)
                                            .addComponent(btn_cancel)))
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 138, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGap(28, 28, 28)))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addComponent(jLabel11)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel7)
                .addGap(5, 5, 5)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(cmb_albums, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(lbl_newAlbum, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(13, 13, 13)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txt_author, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txt_picname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txt_desc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel6)
                            .addComponent(txt_keys, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel10)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 23, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cmb_ontologyes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txt_ontologyterm, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btn_ok)
                            .addComponent(btn_cancel))
                        .addGap(21, 21, 21))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(54, 54, 54)
                        .addComponent(jLabel1)
                        .addContainerGap())))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btn_cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_cancelActionPerformed
        // TODO add your handling code here:
        dispose();
}//GEN-LAST:event_btn_cancelActionPerformed

    private void btn_okActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_okActionPerformed
        // TODO add your handling code here:
        if (txt_picname.getText().length() == 0) {
            jLabel11.setText("Error: Name Field Required");
        } else {

                jLabel11.setText("");
                Picture nueva = new Picture();
                nueva.setName(txt_picname.getText());
                FileDescriptor descriptor = new FileDescriptor();
                descriptor.setPath(path);
                File file = new File(path);
                descriptor.setSize(file.length());
                descriptor.setHash(file.hashCode() + "");
                nueva.setFileDescriptor(descriptor);
                nueva.addAuthor(txt_author.getText());
                TextDescription t = new TextDescription();
                t.setDescription(txt_desc.getText());

                String keys[] = txt_keys.getText().split(",");
                for (String key : keys) {
                    t.addUserKeyword(key.trim());
                }
                
                if (txt_ontologyterm.getText().length() > 0) {
                    Tag tag = new Tag();
                    tag.setOntology((String) cmb_ontologyes.getSelectedItem());
                    tag.setContent(txt_ontologyterm.getText());
                    t.addTag(tag);
                }

                nueva.setTextDescription(t);
                nueva = PictureController.getById(PictureController.createOrUpdate(nueva));

                String myalbum;
                if (lbl_newAlbum.getText().length() > 0) {

                    album = new Album();
                    boolean existe = this.existAlbum(lbl_newAlbum.getText());
                    if (!existe) {
                        album.setName(lbl_newAlbum.getText());
                        auxiliars = true;
                    } else {
                        jLabel11.setText("Error: The Album Introduced exist");
                        auxiliars = false;
                    }
                } else {

                    Vector<Album> auxiliar = GUILogic.getListAlbums();

                    for (int j = 0; j < auxiliar.size(); j++) {
                        myalbum = (String) cmb_albums.getSelectedItem();

                        if (myalbum.equals(auxiliar.get(j).getName())) {

                            album = auxiliar.get(j);
                            j = auxiliar.size();

                            auxiliars = true;
                        }
                    }


                }
                if (auxiliars) {
                    album.addPicture(nueva.getId());
                    album = AlbumController.getById(AlbumController.createOrUpdate(album));
                    im.UpdateAlbums();

                    txt_author.setText("");
                    txt_picname.setText("");
                    txt_desc.setText("");
                    txt_keys.setText("");
                    lbl_newAlbum.setText("");
                    txt_ontologyterm.setText("");
                    dispose();
                }
            




        }

}//GEN-LAST:event_btn_okActionPerformed

    public boolean existAlbum(String name) {
        String myalbum;
        boolean retorno = false;
        Vector<Album> auxiliar = GUILogic.getListAlbums();

        for (int j = 0; j < auxiliar.size(); j++) {
            myalbum = auxiliar.get(j).getName();
    
            if (myalbum.equals(name)) {
                retorno = true;
                j = auxiliar.size();
            }
            if (j == auxiliar.size() - 1) {
                retorno = false;
            }
        }
        return retorno;
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                //   new AddPicAlbum().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_cancel;
    private javax.swing.JButton btn_ok;
    private javax.swing.JComboBox cmb_albums;
    private javax.swing.JComboBox cmb_ontologyes;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JTextField lbl_newAlbum;
    private javax.swing.JTextField txt_author;
    private javax.swing.JTextField txt_desc;
    private javax.swing.JTextField txt_keys;
    private javax.swing.JTextField txt_ontologyterm;
    private javax.swing.JTextField txt_picname;
    // End of variables declaration//GEN-END:variables
}
